<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
        * @param {number} n
        * @param {number} k
        * @return {number[][]}
        */
        var combine = function (n, k) {
            // 回溯法
            let result = [],
                path = [];
            let backtracking = (_n, _k, startIndex) => {
                // 终止条件
                if (path.length === _k) {
                    result.push(path.slice());
                    return;
                }
                // 循环本层集合元素
                for (let i = startIndex; i <= _n; i++) {
                    path.push(i);
                    //   递归
                    backtracking(_n, _k, i + 1);
                    //   回溯操作
                    path.pop();
                }
            };
            backtracking(n, k, 1);
            return result;
        };
    </script>
</body>

</html>